<?php
namespace Nawa\Survey;
/**
 * Survey Section
 * 
 * A survey can be split into different section, each off them
 * will have a title, can have an introduction and a conclusion.
 * You can also attach sub section to your section
 * 
 * @author Xavier VILAIN
 * @package Nawa\Survey
 * @license
 * 
 * @property string 	$surveyid		Id of the Survey
 * @property int 		$sectionid		Id of the Section
 * @property int 		$language		Language
 * @property bool 		$parentid		Id of the parent
 * @property int 		$position		Order in the survey
 * @property int 		$type			Section Type [Not Used Yet]
 * @property text		$label			Label
 * @property text		$description	Description Message
 * @property text		$introduction	Introduction Message
 * @property text		$conclusion		Conclusion Message
 * 
 */
class Section extends \Fox\Object {
	
	/**
	 * Link to question
	 * @var string
	 */
	const LINK_QUESTIONS = 'questions';

	/**
	 * Link to mqc
	 * @var string
	 */
	const LINK_SURVEY = 'survey';
	
	/**
	 * Link to Translation
	 * @var string
	 */
	const LINK_I18N = 'i18n';
	
	/**
	 * Process i18n to get the translated value if it exist,
	 * else the system will default to page value
	 * @param string $fieldName
	 * @return string
	 */
	public function i18nParse($fieldName) {
		$language = \Xview::instance()->language();
		$defaultLanguage = $this->language;
		if($language != $defaultLanguage) {
			$i18n = $this->findI18n($this->getInlineId().':'.$language);
			if($i18n->exists()) {
				return $i18n->$fieldName;
			}
			elseif(!empty($defaultLanguage)) {
				return $this->$fieldName;
			}
		}
		return $this->$fieldName;
	}
	
	/**
	 * @return string label
	 */
	public function label() {
		return $this->i18nParse('label');
	}
	
	/**
	 * @return string Description
	 */
	public function description() {
		return $this->i18nParse('description');
	}
	
	/**
	 * @return string Introduction
	 */
	public function introduction() {
		return $this->i18nParse('introduction');
	}	
	
	/**
	 * @return string Conclusion
	 */
	public function conclusion() {
		return $this->i18nParse('conclusion');
	}	
		
	/**
	 * Build the Section Tree
	 * This method is going to load all sections and all questions
	 * associate to each section 
	 * @return \Fox\Node
	 */
	public static function tree($section) {
		$data = $section->getData();
		$data['object'] = $section;
		$data['id'] = $section->getInLineId();
		$sectionNode = new \Fox\Node($data);
		$questionNodes = array();
		foreach($section->questions() as $question) {
			/* @var $question Survey\Question */
			$parentid = $question->parentid;
			$data = $question->getData();
			$data['label'] = $question->label();
			$data['id'] = $question->getInLineId();
			$data['object'] = $question;
			$data['default'] = null;
			$questionNode = new \Fox\Node($data);
			if(!empty($parentid) && !empty($questionNodes[$parentid])) {
				$questionNodes[$question->questionid] = $questionNodes[$parentid]->add($questionNode);
			} else {
				$questionNodes[$question->questionid] = $sectionNode->add($questionNode);
			}
		}
		return $sectionNode;
	}
		
}
\Fox\Dao::infos(Section::className(),new \Fox\Object\Info('survey_sections', \Fox\Databases::DB_APP))
	->addAutoIncrementField('sectionid');

// Link to i18n elements
\Fox\Object\Links::add(\Fox\Dao::link(Section\I18n::className(),Section\I18n::LINK_SECTION)
->to(Section::className(), Section::LINK_I18N)
->on('surveyid','sectionid')
->versionFrom());

// Link To External Question
\Fox\Object\Links::add(\Fox\Dao::link(Question::className(),Question::LINK_SECTION)
	->to(Section::className(),Section::LINK_QUESTIONS)
	->on('surveyid','sectionid')
	->orderBy('position')
);